package Server;

import global.global;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.hcmus.post2server.post2server;

import common.Promotion;
import common.Store;

import config.config;
import db.DBHandle;

public class HandleServer {
	private static String endPoint ="";
	
	
	public static void TestGetPromotion(){
		
		endPoint = "http://vinaecom.com/location_advertisement_mobile/index.php?route=armobile/handleclient/GetListPromotionTest";
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post",config.type_post_update_location);
		//postReciveJson
		try {
				JSONObject json = Post.postReciveJson(endPoint, params);				
				ArrayList<Promotion> lst = Post.parseJSONPromotion(json);
				int a = 5;			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static ArrayList<Promotion> GetListPromotionByStoreId(int store_id){
		
		ArrayList<Promotion> lst = new ArrayList<Promotion>();
		
		endPoint = config.url_get_promotion_by_store;
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post",config.type_post_get_promotion_of_store);
		params.put("store_id",String.valueOf(store_id));		
		//postReciveJson
		try {
				JSONObject json = Post.postReciveJson(endPoint, params);	
				if(json != null){
					lst = Post.parseJSONPromotion(json); 
					return lst;
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return lst;
		
	}
	public static boolean UpdateLocation(String locaion_id_list){		
		endPoint = config.url_update_location;
		Map<String, String> params = new HashMap<String, String>();
		
		params.put("type_post",config.type_post_update_location);
		params.put("regId", global.curUser.getRegId());
		params.put("location_id_list", locaion_id_list);
		try {
			String result = post2server.postRecive(endPoint, params);
			if(result.equals("success")){
				// update local
				boolean re =  DBHandle.UserUpdateLocation(locaion_id_list);
				if(re==true){
					global.curUser.setProvince(locaion_id_list); 
					return true;
				}
				//return true
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean UpdateType(int type){		
		endPoint = config.url_update_type;
		Map<String, String> params = new HashMap<String, String>();
		
		params.put("type_post",config.type_post_update_type);
		params.put("regId",   global.curUser.getRegId());
		params.put("type_id", String.valueOf(type));
		try {
			String result = post2server.postRecive(endPoint, params);
			if(result.equals("success")){
				// update local
				boolean re=  DBHandle.UserUpdateType(type);
				if(re==true){
					global.curUser.setType(type); 
					return true;
				}
				//return true
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static ArrayList<Store> GetListStoreNearPosition(String lat , String lng, String dis){
		ArrayList<Store> lst = new ArrayList<Store>();
		endPoint = config.url_get_store_near_position;
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post", config.type_post_get_store_near_position);
		params.put("lat", lat);
		params.put("lng",lng);
		params.put("dis", dis);
		params.put("regId", global.curUser.getRegDate());
		//params.put("regId","APA91bFa3lORxmKvXOPQuNT8QCr3YU9mmbraNdXG9snXzYyh3Wy-pWJ5Cn11h7Nc23cq0hYqxbUdG7XHKIZ15h3d2DCyyFGXP2q0f_0VqHQkm6kaTEo7RVEudG2Tdw2RKw-DN-Bqi7gaOvRd6SLkMKoWHRjyJSQhQg");// global.curUser.getRegDate());		
		
		JSONObject json = null;
		try {
			json = Post.postReciveJson(endPoint, params);			
			lst = Post.parseJSONStore(json);
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return lst;
	}
		 
	public static ArrayList<Store> GetListStoreSearch(String name , String locationList, int type ){
		ArrayList<Store> lst = new ArrayList<Store>();
		endPoint = config.url_get_store_search;
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post", config.type_post_get_store_search);
		params.put("store_name", name);
		params.put("location_id_list",locationList);
		params.put("type", String.valueOf(type)); 
		params.put("regId", "");//global.curUser.getRegDate());		
		JSONObject json = null;
		try {
			json = Post.postReciveJson(endPoint, params);			
			lst = Post.parseJSONStore(json);
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return lst;
	}
	
	public static ArrayList<Store> GetListStoreByChain(int chain_id){
		ArrayList<Store> lst = new ArrayList<Store>();
		endPoint = config.url_get_store_by_chain;
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post", config.type_post_get_store_by_chain_store_id);		
		params.put("chain_store_id", String.valueOf(chain_id)); 
		params.put("regId", "");//global.curUser.getRegDate());		
		JSONObject json = null;
		try {
			json = Post.postReciveJson(endPoint, params);			
			lst = Post.parseJSONStore(json);
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return lst;
	}
	
	
	public static ArrayList<Promotion> GetListPromotionSearch(String name , String locationList, int type , String from_price , String to_price){
		ArrayList<Promotion> lst = new ArrayList<Promotion>(); 
		endPoint = config.url_get_promotion_search; 
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post", config.type_post_get_promotion_search);
		params.put("promotion_name", name);
		params.put("location_id_list",locationList);
		params.put("type", String.valueOf(type)); 
		params.put("regId", "");//global.curUser.getRegDate());	
		params.put("from_price", from_price); 
		params.put("to_price", to_price); 
		JSONObject json = null;
		try {
			json = Post.postReciveJson(endPoint, params);			
			lst = Post.parseJSONPromotion(json);						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lst;  
	}
	/* begin customer */
	public static boolean AddUpdateFavourite(int customer_id,String store_id_list , int type){				
		endPoint = config.url_add_remove_favourite;
		Map<String, String> params = new HashMap<String, String>();		
		params.put("type_post",config.type_post_add_remove_favourite);
		params.put("regId", "");//global.curUser.getRegId());
		params.put("id", "1");//String.valueOf(global.curUser.getId())); 
		params.put("store_id_list", store_id_list); 
		params.put("type", String.valueOf(type)); 		
		try {
			String result = post2server.postRecive(endPoint, params);
			if(result.equals("success")){ 
				// update local
				boolean re =  true;//DBHandle.UserUpdateLocation(locaion_id_list);
				if(re==true){
					//global.curUser.setProvince(locaion_id_list); 
					return true;
				}
				//return true 
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static ArrayList<Store> GetListStoreFavourite(){
		ArrayList<Store> lst = new ArrayList<Store>();
		endPoint = config.url_get_store_favourite;
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post", config.type_post_get_store_favourite);
		params.put("customer_id","47");// String.valueOf((global.curUser.getId())));	
		params.put("regId", "");//global.curUser.getRegDate());		
		JSONObject json = null;
		try {
			json = Post.postReciveJson(endPoint, params);			
			lst = Post.parseJSONStore(json);
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return lst;
	}	
	public ArrayList<Promotion> GetListPromotionHistory(){
		ArrayList<Promotion> lst = new ArrayList<Promotion>(); 
		endPoint = config.url_get_get_promotion_by_id_list;  
		String id_list = DBHandle.GetListPtomotionPushByString();
		Map<String, String> params = new HashMap<String, String>();
		params.put("type_post", config.type_post_get_promotion_by_id_list);
		params.put("id_list", id_list);		
		params.put("regId", "");//global.curUser.getRegDate());	 		
		JSONObject json = null;
		try {
			json = Post.postReciveJson(endPoint, params);			
			lst = Post.parseJSONPromotion(json);						
		} catch (IOException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
		}
		return lst;
	}
	/*end customer*/
	
}
